package com.yueya.basic.role.service;

import java.util.Map;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.yueya.basic.role.model.Role;
import com.yueya.common.util.DateUtil;
import com.yueya.common.util.UUIDGenerator;

/**
 * 
 * 
 * @author Administrator
 * 
 */
public class RoleService {

	/**
	 * 分页列表
	 * 
	 * @param pageNumber
	 * @param pageSize
	 * @param map
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public Page<Record> pagination(int pageNumber, int pageSize, Map map) {

		String select = "select * ";
		StringBuffer sb = new StringBuffer(" from t_role a where 1=1 ");

		if (map.get("delflag") != null && !"".equals(map.get("delflag"))) {
			sb.append(" and a.DEL_FLAG = '" + map.get("delflag") + "' ");
		}

		if (map.get("name") != null && !"".equals(map.get("name"))) {
			sb.append(" and role_name like '%" + map.get("name") + "%'");
		}

		sb.append("  order by a.order_id asc,a.status desc,a.create_time asc ");

		String sql = sb.toString();
		Page<Record> page = Db.paginate(pageNumber, pageSize, select, sql);

		return page;

	}

	/**
	 * 保存
	 * 
	 * @param role
	 * @return
	 */
	@Before(Tx.class)
	public boolean save(Role role) {
		boolean bl = false;
		if (role.getStr("ID") != null && !"".equals(role.getStr("ID"))) {
			role.set("modify_time", DateUtil.getDate(DateUtil.timeFormat));
			bl = role.update();
		} else {
			role.set("ID", UUIDGenerator.getUUID());
			role.set("DEL_FLAG", "0");
			role.set("CREATE_TIME", DateUtil.getDate(DateUtil.timeFormat));
			bl = role.save();
		}

		return bl;
	}

	@Before(Tx.class)
	public boolean removeRole(String id) {
		int num = Db.update("update t_role set del_flag='1' where id =?", id);
		if (num > 0) {
			return true;
		}
		return false;

	}

	@Before(Tx.class)
	public boolean bathremove(String idsStr) {
		boolean bl = false;
		if (idsStr != null) {
			String[] ids = idsStr.split(";");
			Role role = null;
			for (int i = 0; i < ids.length; i++) {
				if (!"".equals(ids[i]) && ids[i] != null) {
					role = Role.dao.findById(ids[i]);
					role.set("del_flag", 1);
					if (role.update()) {
						bl = true;
					} else {
						bl = false;
						break;
					}
				}
			}
		}
		return bl;
	}

}
